package com.ts.service.achievement.impl;

import com.ts.dto.TeachingResearchProjectDTO;
import com.ts.dto.MemberDTO;
import com.ts.entity.TeachingResearchProject;
import com.ts.entity.Member;
import com.ts.mapper.achievement.TeachingResearchProjectMapper;
import com.ts.mapper.achievement.TeachingResearchProjectMemberMapper;
import com.ts.service.achievement.TeachingResearchProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TeachingResearchProjectServiceImpl implements TeachingResearchProjectService {

    @Autowired
    private TeachingResearchProjectMapper teachingResearchProjectMapper;

    @Autowired
    private TeachingResearchProjectMemberMapper teachingResearchProjectMemberMapper;

    /**
     * 提交一个教研项目类成果
     */
    @Transactional
    @Override
    public void save(TeachingResearchProjectDTO teachingResearchProjectDTO) {
        // 1.准备数据
        TeachingResearchProject teachingResearchProject = new TeachingResearchProject();
        BeanUtils.copyProperties(teachingResearchProjectDTO, teachingResearchProject);

        // 2.手动添加公共字段
        teachingResearchProject.setCreateTime(LocalDateTime.now());
        teachingResearchProject.setUpdateTime(LocalDateTime.now());
        teachingResearchProject.setCreateBy(1L);
        teachingResearchProject.setUpdateBy(1L);

        // 3.保存教研项目主表
        teachingResearchProjectMapper.save(teachingResearchProject);

        // 4.处理成员信息
        saveMembers(teachingResearchProjectDTO.getMemberList(), teachingResearchProject.getId());
    }

    /**
     * 根据ID查询教研项目详情
     */
    @Override
    public TeachingResearchProjectDTO getById(Long id) {
        // 1.查询项目主表
        TeachingResearchProject project = teachingResearchProjectMapper.getById(id);
        if (project == null) {
            return null;
        }

        // 2.转换为DTO
        TeachingResearchProjectDTO dto = new TeachingResearchProjectDTO();
        BeanUtils.copyProperties(project, dto);

        // 3.查询成员信息
        List<Member> members = teachingResearchProjectMemberMapper.getByProjectId(id);
        List<MemberDTO> memberDTOs = members.stream()
                .map(member -> {
                    MemberDTO memberDTO = new MemberDTO();
                    BeanUtils.copyProperties(member, memberDTO);
                    return memberDTO;
                })
                .collect(Collectors.toList());

        dto.setMemberList(memberDTOs);
        return dto;
    }

    /**
     * 更新教研项目
     */
    @Transactional
    @Override
    public void update(TeachingResearchProjectDTO teachingResearchProjectDTO) {
        // 1.更新项目主表
        TeachingResearchProject teachingResearchProject = new TeachingResearchProject();
        BeanUtils.copyProperties(teachingResearchProjectDTO, teachingResearchProject);
        teachingResearchProject.setUpdateTime(LocalDateTime.now());
        teachingResearchProject.setUpdateBy(1L);

        teachingResearchProjectMapper.update(teachingResearchProject);

        // 2.先删除原有成员，再重新插入
        teachingResearchProjectMemberMapper.deleteByProjectId(teachingResearchProject.getId());

        // 3.插入新的成员信息
        saveMembers(teachingResearchProjectDTO.getMemberList(), teachingResearchProject.getId());
    }

    /**
     * 根据ID删除教研项目
     */
    @Transactional
    @Override
    public void delete(Long id) {
        // 先删除成员信息（外键级联删除也会自动处理）
        teachingResearchProjectMemberMapper.deleteByProjectId(id);
        // 再删除项目主表
        teachingResearchProjectMapper.delete(id);
    }

    /**
     * 保存成员信息的私有方法
     */
    private void saveMembers(List<MemberDTO> memberDTOs, Long projectId) {
        if (memberDTOs != null && !memberDTOs.isEmpty()) {
            List<Member> members = memberDTOs.stream()
                    .map(memberDTO -> {
                        Member member = new Member();
                        BeanUtils.copyProperties(memberDTO, member);
                        member.setProjectId(projectId);
                        return member;
                    })
                    .collect(Collectors.toList());

            // 批量保存成员信息
            teachingResearchProjectMemberMapper.batchInsert(members);
        }
    }
}